// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <algorithm>
#include <functional>
#include <string>
#include <vector>

namespace kumo::pollux::dwio::catalog::hive {
    class FileUtils {
    public:
        /// Converts the path name to be hive metastore compliant, will do
        /// url-encoding when needed.
        static std::string escapePathName(const std::string &data);

        /// Converts the hive-metastore-compliant path name back to its original path
        /// name.
        static std::string unescapePathName(const std::string &data);

        /// Creates the partition directory path from the list of partition key/value
        /// pairs, will do url-encoding when needed.
        static std::string makePartName(
            const std::vector<std::pair<std::string, std::string> > &entries,
            bool partitionPathAsLowerCase);

        /// Converts the hive-metastore-compliant path name back to the corresponding
        /// partition key/value pairs.
        static std::vector<std::pair<std::string, std::string> > parsePartKeyValues(
            const std::string &partName);

        /// Converts a path name to a hive-metastore-compliant path name.
        static std::string extractPartitionName(const std::string &filePath);

        inline static const std::string kDefaultPartitionValue =
                "__HIVE_DEFAULT_PARTITION__";
    };
} // namespace kumo::pollux::dwio::catalog::hive
